package com.webbei.HAO.dao.impl;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.webbei.HAO.dao.ChDao;
import com.webbei.pojo.Brands;
import com.webbei.pojo.Carseries;
import com.webbei.pojo.Cartypes;
import com.webbei.pojo.Customer;
import com.webbei.pojo.Customerlimit;
import com.webbei.pojo.ThirdPartyInfo;

public class ChDaoimpl implements ChDao {
	private SqlSession session;

	public List<Brands> list() {
		List<Brands> list = session.selectList("Brands.getBrands");
		return list;
	}

	public List<Carseries> carlist(int id) {
		List<Carseries> list = session.selectList("Carseries.getCarseries", id);
		return list;
	}

	public List<Customer> cuslist() {
		List<Customer> list = session.selectList("customer.getUsername");
		return list;
	}

	public int customercheck(String email){
		return (Integer) session.selectOne("customer.getCustomerCount", email);
	}

	public int customercheck2(String name){
		return (Integer) session.selectOne("customer.getCustomerCount2", name);
	}

	public SqlSession getSession() {
		return session;
	}

	public void setSession(SqlSession session) {
		this.session = session;
	}

	public boolean update(int userid, String username, String realname,
			String file, String sex, String provaice, String city,
			String address,String area, String telphone, String phone) {
		Customer cus = new Customer();
		cus.setUserId(userid);
		cus.setUserName(username);
		cus.setUserRealName(realname);
		cus.setUserFace(file);
		cus.setUserSex(sex);
		cus.setUserProvince(provaice);
		cus.setUserCity(city);
		cus.setUserArea(area);
		cus.setUserAddress(address);
		cus.setUsertelePhone(phone);
		cus.setUserMobilePhone(telphone);
		int i = session.update("customer.updateCustomer", cus);
		if (i != 0)
			return true;
		else
			return false;
	}

	public void createfile(String userpath) {
		System.out.println(userpath+"\n-----------------------");
		File file = new File(userpath);
		if (!file.exists()) {
			file.mkdirs();
		}
		File file1=new File(userpath+"/userCDR");
		File file2=new File(userpath+"/userCustom");
		File file3=new File(userpath+"/userLovecar");
		File file4=new File(userpath+"/userTouxiang");
		file1.mkdirs();
		file2.mkdirs();
		file3.mkdirs();
		file4.mkdirs();
	}

	public int getUidbyEmail(String mail) {
		int uid = (Integer) session.selectOne("customer.getUid", mail);
		return uid;
	}

	/*public boolean insertUname(String uname, int id, String userface) {

		Customer cus = new Customer();
		cus.setUserName(uname);
		cus.setUserId(id);
		cus.setUserFace(userface);
		int i = session.update("customer.updateuname", cus);
		if (i != 0)
			return true;
		else
			return false;
	}*/

	@Override
	public String getMailbyUid(int id) {
		String mail = (String) session.selectOne("customer.getMail", id);
		return mail;
	}

	@Override
	public Customer cus(int id) {
		Customer cu = (Customer) session.selectOne("Carseries.getCustomer", id);
		return cu;
	}

	@Override
	public boolean updatePwdbyEmail(String mail, String pwd) {
		Customer cus = new Customer();
		cus.setUserPwd(pwd);
		cus.setUserMail(mail);
		int i = session.update("customer.resetpwd", cus);
		if (i != 0)
			return true;
		else
			return false;
	}

	@Override
	public String getPwdbyUid(int id) {
		String pwd = (String) session.selectOne("customer.getPwd", id);
		return pwd;
	}

	@Override
	public boolean updatePwdbyId(int id, String pwd) {
		Customer cus = new Customer();
		cus.setUserPwd(pwd);
		cus.setUserId(id);
		int i = session.update("customer.updatepwd2", cus);
		if (i != 0)
			return true;
		else
			return false;

	}

	@Override
	public int register(String mail, String password,String uname,String userface,String nickname) {
		Customer cus = new Customer();
		cus.setUserMail(mail);
		cus.setUserPwd(password);
		cus.setUserName(uname);
		cus.setUserFace(userface);
		cus.setNickname(nickname);
		System.out.println(cus+"--------------register");
		int i = session.insert("customer.registcus", cus);
		int userid = (Integer) session.selectOne("customers.getMaxId");

		System.out.println("userid:---------"+userid);
		if (i != 0)
			return userid;
		else
			return -1;
	}

	public String getPwdByMail(String mail){
		return (String) session.selectOne("customer.getPwdbyMail",mail);
	}


	public List<Cartypes> cartypeNamelist(String seriesName) {
		String typesName=seriesName;
		List<Cartypes> list=session.selectList("cartypes.gettypesPic",typesName);
		return list;
	}

	public int insertcuslimit(String uname) {
		Customerlimit limi=new Customerlimit();
		limi.setUserName(uname);
		return session.insert("cuslimit.insertall", limi);
	}

	@Override
	public String LoginTheWebBei(Map<String, String> m) {
		// TODO Auto-generated method stub
		 String name=(String)session.selectOne("customer.loginwb",m);
		 return name;
	}

	@Override
	public int insertThirdParty(Map map) {
		// TODO Auto-generated method stub

		return session.insert("thirdparty.insertThirdParty",map);
	}

	@Override
	public String getNameByOpenId(String openid) {
		System.out.println(openid);
		String s=(String)session.selectOne("thirdparty.getNameByOpenId",openid);
		System.out.println(s);
		System.out.println(123);
		return s;
	}
	@Override
	public int checkEmail(String email) {
		// TODO Auto-generated method stub
		return (Integer)session.selectOne("customer.checkemail", email);
	}

	@Override
	public void insertUserhot(String username,String face) {
		Map map=new HashMap();
		map.put("name", username);
		map.put("face", face);
		session.insert("customer.insertUserhot",map);

	}

	@Override
	public void insertLovecar(int userid) {
		session.insert("customer.insertLovecar",userid);

	}

	@Override
	public void updatePersonalSign(String username, String personalSign) {
		// TODO Auto-generated method stub
		Map m=new HashMap();
		m.put("name", username);
		m.put("sign", personalSign);
		session.update("userhot.updatePersonalSign",m);
	}

	@Override
	public int getProductIdByUrl(String bgPic) {
		return (Integer)session.selectOne("BgPic.getProductIdByUrl",bgPic);
	}

	@Override
	public List getBgPicByUrl(String bgPic) {
		// TODO Auto-generated method stub
		return session.selectList("BgPic.getBgPicByUrl2",bgPic);
	}

	@Override
	public List<ThirdPartyInfo> getThirdPartyByUsername(String username) {
		// TODO Auto-generated method stub
		return session.selectList("thirdPartyInfo.getThirdPartyByUsername",username);
	}

	@Override
	public ThirdPartyInfo getThirdPartyById(Object thirdTypeId) {
		// TODO Auto-generated method stub
		int id = (Integer)thirdTypeId;
		return (ThirdPartyInfo) session.selectOne("thirdPartyInfo.getThirdPartyById",thirdTypeId);
	}

	@Override
	public int deleteThirPartyById(int id) {
		// TODO Auto-generated method stub
		return session.delete("thirdPartyInfo.deleteThirPartyById",id);
	}

	public void update(Customer cus) {
		// TODO Auto-generated method stub
		session.update("customer.updateCustomer", cus);
	}
	@Override
	public void updateClickCountByProductId(int productId) {
	    session.update("Products.updateClickCountByProductId",productId);
	}


}
